function MakeDARISuserpreferencefile(Username,CurrentServer)
% Create or modify DaRIS user preference file (~/.daris)
%

%     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
%
%     This file is part of Xglom.
% 
%     This is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.


if nargin < 1
    Username = 'YourAuthcateUsername';
end
if nargin < 2 
    CurrentServer='MON-DARIS';
end
if ~exist('~/.daris','file')
%if ~isempty(strfind(messageid, MATLAB:FILEATTRIB:CannotFindFile))
    status = system(['echo "mf-erc:mf-erc.its.monash.edu.au:https:8443:monash-ldap:' Username ' :MBI-TOAND:MON-DARIS:6666" > ~/.daris']);
    return;
end
if ~exist('~/.daris','file')
    %[attrib message messageid] = fileattrib('~');
    %if ~message.UserRead && ~message.UserWrite
    disp('DarisDialog Error:  you do not have read/write access to files in your home directory,  Seek help from your system administrator.');
    return;
    %end
end


% Check for file and MON-DARIS (vera059 replaced by mf-erc)
if ~isempty(strfind(CurrentServer,'MON-DARIS'))
    status = system('grep -e ''mf-erc*MON-DARIS'' ~/.daris');
    if status ~= 0
        disp('Making default ~/.daris file.');
        status = system(['echo "mf-erc:mf-erc.its.monash.edu.au:https:8443:monash-ldap:' Username ':MBI-TOAND:MON-DARIS:6666\n" > ~/.daris']);
        
    else
        status = system('grep vera059 ~/.daris');
        disp('Replacing with mf-erf in ~/.daris file.');
        status = system(['sed -i ''s/vera059/mf-erc/g''  ~/.daris']);
    
    end
end    

    % Change existing username
    disp(['Changing existing username in ~/.daris. Current server ' CurrentServer]);
    [status uname] = system(['awk -F: ''/' CurrentServer '/ {print $6}'' < ~/.daris']);
    uname  = regexp(uname,'\n','split');
    CurrentUsername = uname{1};
    disp(['Changing username ' CurrentUsername ' to ' Username ])
    [status output] =system ([' sed -i ''/' CurrentServer '/s/' CurrentUsername '/' Username '/'' ~/.daris'])

